package com.woniuxy.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.Columns;
import com.woniuxy.service.ColumnsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/column")
public class ColumnsController {

    @Autowired
    private ColumnsService columnsService;
    @GetMapping("/getAllColumns")
    public ResponseEntity<?> getAllColumns() {
        List<Columns> columns = columnsService.getAllColumns();
        return ResponseEntity.ok(columns);
    }
    @GetMapping("/getColumnById/{id}")
    public ResponseEntity<?> getColumnById(@PathVariable Integer id){
        Columns column = columnsService.getColumnById(id);
        return ResponseEntity.ok(column);
    }
    @PutMapping("/changeEnabled/{id}/{enabled}")
    public ResponseEntity<?> changeEnabled(@PathVariable Integer id,
                                           @PathVariable Integer enabled){
        LambdaUpdateWrapper<Columns> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Columns::getId,id).set(Columns::getEnabled,enabled);
        columnsService.update(wrapper);
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
    @DeleteMapping("/batchDelete")
    public ResponseEntity<?> batchDelete(@RequestBody List<Integer> ids){
        columnsService.removeByIds(ids);
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
    @DeleteMapping("/deleteColumn/{id}")
    public ResponseEntity<?> deleteColumn(@PathVariable Integer id){
        columnsService.removeById(id);
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
    @PostMapping("/saveOrUpdateColumn")
    public ResponseEntity<?> saveColumn(@RequestBody Columns columns){
        columns.setEnabled(0);
        columnsService.saveOrUpdate(columns);
        return ResponseEntity.ok(SystemCode.SUCCESS.getMessage());
    }
}
